package models.my;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import models.user.UserInfo;
import play.Logger;
import play.db.ebean.Model;
import tyrex.services.UUID;
import utils.MengException;
import utils.StringUtils;
//19
/**
 * 我的帖子
 * @author zhangpeng
 *
 */

@Entity
@Table(name="my_posts")
public class MyPosts extends Model{

	private static final long serialVersionUID = 1L;
	
	@Id
	public long id;
	public String username;
	public String code = UUID.create();
	public String title;
	public String content;
	public String pic_url;      //图片链接
	public String video_url;    //视频链接
	public long create_at = StringUtils.getTimeStamp();
	
	public static MyPosts getInstances(){
		return new MyPosts();
	}
	
	public static Model.Finder<Long, MyPosts> find = new Model.Finder<Long, MyPosts>(Long.class, MyPosts.class);
	
	/**
	 * 创建用户帖子
	 * 
	 * @param posts
	 * @return
	 * @throws MengException
	 */
	public boolean createPosts(MyPosts posts) throws MengException {

		if (UserInfo.getInstances().verifyUsername(posts.username)) {
			throw new MengException("190001", "用户名不存在");
		}

		if (!verifyMyPosts(posts.code)) {
			throw new MengException("190002", "用户帖子已存在");
		}

		try {
			posts.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createPosts Exception," + t.getMessage());
			throw new MengException("190003", "用户帖子保存失败");
		}
	}
	
	/**
	 * 修改用户帖子
	 * @param posts
	 * @return
	 * @throws MengException
	 */
	public boolean modifyPosts(MyPosts posts) throws MengException {
		
		if (UserInfo.getInstances().verifyUsername(posts.username)) {
			throw new MengException("190004", "用户名不存在");
		}

		if (verifyMyPosts(posts.code)) {
			throw new MengException("190005", "用户帖子不存在");
		}
		posts.id = getPosts(posts.code).id;
		try {
			posts.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo modifyPosts Exception," + t.getMessage());
			throw new MengException("190006", "用户帖子修改失败");
		}
	}
	
	/**
	 * 删除用户帖子
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public boolean destroyPosts(String code) throws MengException {
		
		if (verifyMyPosts(code)) {
			throw new MengException("190007", "用户帖子不存在");
		}

		try {
			getPosts(code).delete();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo destroyPosts Exception," + t.getMessage());
			throw new MengException("190008", "删除帖子保存失败");
		}
	}
	
	/**
	 * 获取帖子列表
	 * @param username
	 * @param page
	 * @param size
	 * @return
	 * @throws MengException
	 */
	public List<MyPosts> getPostsPage(String username, int page, int size) throws MengException {
		
		try {
			return find.where().eq("username", username).order().desc("create_at").setAutofetch(false).findPagingList(size).getPage(page).getList();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getPostsPage Exception," + t.getMessage());
			throw new MengException("190009", "获取帖子列表失败");
		}
	}
	
	/**
	 * 获取帖子
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public MyPosts getPosts(String code) throws MengException {
		
		try {
			return find.where().eq("code", code).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo getPosts Exception," + t.getMessage());
			throw new MengException("190010", "获取帖子失败");
		}
	}
	
	/**
	 * 验证帖子
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public boolean verifyMyPosts(String code) throws MengException {
		MyPosts posts = find.where().eq("code", code).findUnique();
		if(posts == null) {
			return true;
		}
		return false;
	}

}
